using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

[WebService(Namespace = "logistics")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ketnoi"].ToString());
    public Service()
    {

    }

    //use select sql query
    private DataSet QuerySelect(string sql, string table)
    {
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(sql, conn);
        da.Fill(ds, table);
        return ds;
    }
    //user update delete insert sql query

    private int QueryCommand(string sql)
    {

        try
        {

            conn.Open();
            SqlCommand comm = new SqlCommand(sql, conn);
            int i = comm.ExecuteNonQuery();
            conn.Close();
            return i;
        }
        catch (Exception ex)
        {
            return 0;
        }

    }

    ///////////////////////// START USER WS//////////////////////////////////////

    //login
    [WebMethod]
    public DataSet lgUserInfo(string u)
    {
        return QuerySelect("select * from Users where UserName ='" + u + "'", "user");

    }
   
    [WebMethod]
    public bool lgUserCheckLogin(string u, string p)
    {
        DataSet ds = QuerySelect("select * from Users where UserName='" + u + "' and Password='" + p + "' and Status =1", "user");
        if (ds.Tables["user"].Rows.Count > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    [WebMethod]
    public int lgUserUpdate(string fname, string lname, string phone, string address,string username) {
        string sql = "update users set FirstName='" + fname + "' ,LastName='" + lname + "', Phone='" + phone + "', Address='" + address + "'  where UserName='" + username + "'";
        return QueryCommand(sql);
    }
    [WebMethod]
    public int lgUserPassword(string username,string password){
        string sql = "update users set Password='"+password+"' where UserName='"+username+"'";
        return QueryCommand(sql);
    }

    ///////////////////////// END USER WS//////////////////////////////////////
    ///////////////////////// START WAYBILLS WS//////////////////////////////////////
    [WebMethod]
    public DataSet lgWaybillsGetAll() {
        return QuerySelect("select * from WayBills", "waybills");
    }
    [WebMethod]
    public DataSet lgWaybillsGetAllByUser(string uid,string table) {
        return QuerySelect("select *,[trangthai]= case 	when Status=1 then 'OK'	when Status=0 then 'Pendding' when Status =2 then 'Cancel' end from waybills where UserID='"+uid+"'",table);
    }

    ///////////////////////// END WAYBILLS WS//////////////////////////////////////

}
    
